package com.dragon.mmochat.model;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/* 生成1~Integer.MAX_VALUE-1的序列号
 * 服务重启后此类会重置,ID重新由1开始计数,故此类生成的ID只能用于临时性ID,如战场ID等. 
 * 如果要用于装备ID等永久性ID,需要将此ID值保存到数据库以便下次重启时初始化
 */
public class MmochatSerial implements Serializable {
	private static final long serialVersionUID = -5059696300424402149L;
	private AtomicInteger uniqueId = new AtomicInteger(1);

	// 获取临时Id
	public MmochatSerial() {
	}

	public MmochatSerial(int newValue) {
		uniqueId.set(newValue);
	}

	public AtomicInteger getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(AtomicInteger uniqueId) {
		this.uniqueId = uniqueId;
	}

	public void setUniqueId(int newValue) {
		uniqueId.set(newValue);
	}

	public int addAndGetSerialId() {
		return uniqueId.incrementAndGet();
	}

	public int subAndGetSerialId() {
		return uniqueId.decrementAndGet();
	}

	public int getSerialId() {
		return uniqueId.get();
	}
}
